package com.briup.jz.service.impl;

import java.util.List;

import org.apache.ibatis.cache.CacheException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.Category;
import com.briup.jz.bean.CategoryExample;
import com.briup.jz.bean.Order;
import com.briup.jz.bean.OrderExample;
import com.briup.jz.dao.CategoryMapper;
import com.briup.jz.dao.OrderMapper;
import com.briup.jz.service.IOrderService;
import com.briup.jz.utils.CustomerException;
@Service
public class OrderServiceImpl implements IOrderService{
	@Autowired
	private OrderMapper orderMapper;
	
	@Override
	public void savaOrUpdate(Order order) throws CacheException {
		if(order.getId()!=null) {
			orderMapper.updateByPrimaryKey(order);
		} else {//判断插入数据的
			// 判断是否有同名的分类，如果有抛出异常
			OrderExample example = new OrderExample();
			example.createCriteria().andOrderTimeEqualTo(order.getOrderTime());
			List<Order> list = orderMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名的分类已存在");	
			}
			orderMapper.insert(order);
		}
	}
	@Override
	public List<Order> query (String status) {
		OrderExample example = new OrderExample();
		if(status!=null){
			example.createCriteria().andStatusLike("%"+status+"%");
		}
		List<Order> list = orderMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public void deleteById(long id) throws CustomerException {
		Order order = orderMapper.selectByPrimaryKey(id);
		if(order == null) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		orderMapper.deleteByPrimaryKey(id);
		
	}

	

}
